import lib_activity from "../../api/lib_activity"
import lib_clue from "../../api/lib_clue"
import _time from "../../utils/_time"
import _object from "../../utils/_object";

Page({
  data: {
    activity_id: 0, //活动ID
    tids: '', //模板id
    tid: 1, // 5是非汽车行业
    id: 0,
    channel: 0, //渠道ID
    ui: {}, //模板信息
    title: '', //
    share_theme: '', //分享主题
    theme: '',
    stime: 0,
    etime: 0,
    stime_etime: '', // 活动时间
    address: '', // 活动地点
    is_stop: false,
    name: '', //姓名
    phone: undefined, //电话
    brand: '', //车型
    is_brand: false, //是否有车型
  },
  onLoad: function (options) {
     //项目ID参数
     if (_object._length(options) > 0 && options.hasOwnProperty('activity_id') && options.activity_id > 0) {
      //设置项目缓存
      this.setData({
        tids: options.activity_id
      })
    }
     //项目ID参数
     if (_object._length(options) > 0 && options.hasOwnProperty('tids') && options.tids > 0) {
      //设置项目缓存
      this.setData({
        tids: options.tids
      })
    }
     //项目ID参数
     if (_object._length(options) > 0 && options.hasOwnProperty('channel') && options.channel > 0) {
      //设置项目缓存
      this.setData({
        tids: options.channel
      })
    }
         //项目ID参数
     if (_object._length(options) > 0 && options.hasOwnProperty('id') && options.id > 0) {
      //设置项目缓存
      this.setData({
        tids: options.id
      })
    }

    //获取项目信息
    lib_activity.info(this.activity_id, this.tids, this.init_info);

    // 报名信息
    lib_clue.info(this.id, this.init_userinfo)
  },

  init_info(data) {
    console.log(data)
    this.setData({
      share_theme: data.activity.share_theme,
      stime: data.activity.stime,
      etime: data.activity.etime,
      stime_etime: _time._range_date(this.stime * 1000, this.etime * 1000),
      address: data.activity.address,
      tid: data.activity.tid,
      ui: data.ui,
    })
    // 项目结束
    if (!data.activity.state || (_time._current() > data.activity.etime)) {
      this.setData({
        is_stop: true
      })
      return;
    }

    // 分享： ui存在再分享
    if (_object._length(this.data.ui) > 0) {
      this.share();
    }
  },


  init_userinfo(data) {
    this.setData({
      name: data.name,
      phone: data.phone,
    })
    if (data.brand_name || data.series_name) {
      this.setData({
        is_brand: true,
        brand: data.brand_name + ' - ' + data.series_name
      })
    }
  },
  
  
})